package com.cbank.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cbank.constants.Result;
import com.cbank.constants.ResultCode;
import com.cbank.domain.AboutUs;
import com.cbank.domain.AboutUsBanner;
import com.cbank.service.AboutUsBannerService;
import com.cbank.service.AboutUsService;
import com.cbank.utils.JSONUtil;
import com.cbank.utils.StringUtil;
import com.cbank.vo.AboutUsVo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/aboutus")
public class AboutUsController {
    Logger logger=Logger.getLogger(AboutUsController.class);

    @Autowired
    private AboutUsService aboutUsService;
    @Autowired
    private AboutUsBannerService aboutUsBannerService;

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public Result saveAboutUs(@RequestParam(value = "cnArticle",required = false)String cnArticle,@RequestParam(value = "enArticle",required = false)String enArticle,
                              @RequestParam(value = "imageText",required = false)String imageText,@RequestParam(value = "aboutUsId",required = false,defaultValue = "0")String aboutUsId){
            AboutUs aboutUs = this.aboutUsService.getById(StringUtil.toIntValue(aboutUsId));
            if (aboutUs == null) {
                AboutUs aboutUsDb = new AboutUs();
                aboutUsDb.setCnArticle(cnArticle);
                aboutUsDb.setEnArticle(enArticle);
                this.aboutUsService.save(aboutUsDb);
            } else {
                aboutUs.setEnArticle(enArticle);
                aboutUs.setCnArticle(cnArticle);
                this.aboutUsService.save(aboutUs);
            }
            if (StringUtil.isNotBlank(imageText)) {
                JSONArray images = JSONUtil.parseArray(imageText);
                for (int i = 0; i < images.size(); i++) {
                    JSONObject image = JSONUtil.parseObject(images.get(i).toString());
                    AboutUsBanner aboutUsBanner = new AboutUsBanner();
                    aboutUsBanner.setId(image.getInteger("id"));
                    aboutUsBanner.setUrl(image.getString("url"));
                    //aboutUsBanner.setTitle(image.getString("title"));
                    aboutUsBanner.setAboutUsId(StringUtil.toIntValue(aboutUs.getId()));
                    this.aboutUsBannerService.save(aboutUsBanner);
                }
            }
            return new Result(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg());
    }

    @RequestMapping(value = "/getAboutUs",method = RequestMethod.GET)
    @ResponseBody
    public Result<AboutUsVo> getAboutUs(){
        AboutUs aboutUs=this.aboutUsService.getAll();
        if (aboutUs == null){
            AboutUsVo aboutUsVo=new AboutUsVo();
            aboutUsVo.setId(0);
            aboutUsVo.setAboutUsBannerList(new ArrayList());
            aboutUsVo.setCnArticle(StringUtil.EMPTY_STRING);
            aboutUsVo.setEnArticle(StringUtil.EMPTY_STRING);
            return new Result<AboutUsVo>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMsg(),aboutUsVo);
        }else {
            List<AboutUsBanner> aboutUsBannerList=this.aboutUsBannerService.getByAboutUsId(aboutUs.getId());
            AboutUsVo aboutUsVo = new AboutUsVo();
            aboutUsVo.setId(aboutUs.getId());
            aboutUsVo.setAboutUsBannerList(aboutUsBannerList);
            aboutUsVo.setCnArticle(aboutUs.getCnArticle());
            aboutUsVo.setEnArticle(aboutUs.getEnArticle());
            return new Result<AboutUsVo>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), aboutUsVo);
        }
    }
}
